//
//  String+ex.swift
//  WZApp
//
//  Created by evol on 2018/7/3.
//  Copyright © 2018年 evol. All rights reserved.
//

import UIKit

// 转拼音
extension String {
    var el_pinyin:String {
        get {
            let mutableString = NSMutableString(string: self)
            //把汉字转为拼音
            CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
            //去掉拼音的音标
            CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
            
            let pinyin = String(mutableString)
            var firstCharacters = ""
            for estr in pinyin.components(separatedBy: " ") {
                if (estr.first != nil) {
                    firstCharacters.append(estr.first!)
                }
            }
            return pinyin + firstCharacters
        }
    }
}

extension String {
    var floatValue: CGFloat {
        get {
            return CGFloat((self as NSString).floatValue)
        }
    }
}

// 图片缓存
extension String {
    var imageCachePath: String {
        get {
            let key = self.components(separatedBy: "/").last
            let homeDirectory  = NSHomeDirectory()
            let imageDirectory = homeDirectory.appending("Library/Caches/image_cuts_cache")
            let fileManager = FileManager.default
            var directory: ObjCBool = ObjCBool(false)
            let dirExist = fileManager.fileExists(atPath: imageDirectory, isDirectory: &directory)
            if !dirExist || !directory.boolValue {
                try? fileManager.createDirectory(at: URL(string: imageDirectory)!, withIntermediateDirectories: true, attributes: nil)
            }
            return imageDirectory + "/" + key!
        }
    }
}

